package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.button.StateButton;

/**
 * description: 首页 ==> 血糖
 * created by kalu on 2018/3/29 9:03
 */
public class TagDialog extends BaseDialog implements View.OnClickListener, TextWatcher {

    private String str = "午餐前";
    /***************************************************/

    private OnDialogChangeListener listener;

    public TagDialog(Activity activity) {
        super(activity);
    }

    public TagDialog(Activity activity, int themeResId) {
        super(activity, themeResId);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_bloodglucose;
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    protected void initData() {

        findViewById(R.id.activity_home_bloodglucose_detail_dialog_back).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_ok).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_1).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_2).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_3).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_4).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_5).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_6).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_7).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_8).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_10).setOnClickListener(this);
        findViewById(R.id.activity_home_bloodglucose_detail_dialog_9).setOnClickListener(this);
        ((EditText) findViewById(R.id.activity_home_bloodglucose_detail_dialog_9)).addTextChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_home_bloodglucose_detail_dialog_back:
                dismiss();
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_ok:
                if (null == listener || TextUtils.isEmpty(str)) break;
                listener.onDialogChange(str);
                dismiss();
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_1:
                str = "早餐前";
                setBackgroundColor(1);
                EditText edit1 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit1) {
                    edit1.setFocusable(false);
                    edit1.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit1);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_2:
                str = "早餐后";
                setBackgroundColor(2);
                EditText edit2 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit2) {
                    edit2.setFocusable(false);
                    edit2.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit2);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_3:
                str = "睡前";
                setBackgroundColor(3);
                EditText edit3 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit3) {
                    edit3.setFocusable(false);
                    edit3.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit3);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_4:
                str = "空腹";
                setBackgroundColor(4);
                EditText edit4 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit4) {
                    edit4.setFocusable(false);
                    edit4.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit4);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_5:
                str = "午餐前";
                setBackgroundColor(5);
                EditText edit5 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit5) {
                    edit5.setFocusable(false);
                    edit5.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit5);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_6:
                str = "午餐后";
                setBackgroundColor(6);
                EditText edit6 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit6) {
                    edit6.setFocusable(false);
                    edit6.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit6);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_7:
                str = "晚餐前";
                setBackgroundColor(7);
                EditText edit7 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit7) {
                    edit7.setFocusable(false);
                    edit7.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit7);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_8:
                str = "晚餐后";
                setBackgroundColor(8);
                EditText edit8 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit8) {
                    edit8.setFocusable(false);
                    edit8.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit8);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_10:
                str = "凌晨";
                setBackgroundColor(10);
                EditText edit10 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit10) {
                    edit10.setFocusable(false);
                    edit10.setFocusableInTouchMode(false);
                    InputUtil.closeKeybord(edit10);
                }
                break;
            case R.id.activity_home_bloodglucose_detail_dialog_9:
                setBackgroundColor(9);
                EditText edit9 = findViewById(R.id.activity_home_bloodglucose_detail_dialog_9);
                if (null != edit9) {
                    edit9.setFocusable(true);
                    edit9.setFocusableInTouchMode(true);
                    InputUtil.openKeybord(edit9);
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        str = editable.toString();
    }

    private final void setBackgroundColor(int index) {

        final Context context = getContext().getApplicationContext();
        final int theme = context.getResources().getColor(R.color.color_bg_theme);
        final int white = context.getResources().getColor(R.color.color_white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_1)).setNormalBackgroundColor(index == 1 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_1)).setNormalTextColor(index == 1 ? white : theme);

        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_2)).setNormalBackgroundColor(index == 2 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_2)).setNormalTextColor(index == 2 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_3)).setNormalBackgroundColor(index == 3 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_3)).setNormalTextColor(index == 3 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_4)).setNormalBackgroundColor(index == 4 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_4)).setNormalTextColor(index == 4 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_5)).setNormalBackgroundColor(index == 5 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_5)).setNormalTextColor(index == 5 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_6)).setNormalBackgroundColor(index == 6 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_6)).setNormalTextColor(index == 6 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_7)).setNormalBackgroundColor(index == 7 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_7)).setNormalTextColor(index == 7 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_8)).setNormalBackgroundColor(index == 8 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_8)).setNormalTextColor(index == 8 ? white : theme);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_10)).setNormalBackgroundColor(index == 10 ? theme : white);
        ((StateButton) findViewById(R.id.activity_home_bloodglucose_detail_dialog_10)).setNormalTextColor(index == 10 ? white : theme);
    }

    public void setOnDialogChangeListener(OnDialogChangeListener listener) {
        this.listener = listener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String str);
    }
}
